package com.drink.yan.job;

import com.drink.yan.intf.define.YanActivityStatusE;
import com.drink.yan.intf.entity.YanActivity;
import com.drink.yan.intf.vo.ActivityVo;
import com.drink.yan.svc.service.YanActivityService;
import com.drink.yan.web.rest.req.qry.PageActivityQry;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 定时任务，计算活动状态
 *
 * @Title:
 * @Package: com.drink.yan.job.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2023-12-18 12:12
 * @Version: v2.0
 */
@Slf4j
@Component("YanActivityStatusJob")
public class YanActivityStatusJob {

	@Value("${schdule.job.enable}")
	private Boolean schduleJobEnable;

	@Resource
	private YanActivityService yanActivityService;

	public void handle(){

		if(!schduleJobEnable){
			return;
		}

		log.info("===========start YanActivityStatusJob===========");
		PageActivityQry pageActivityQry = new PageActivityQry();
		pageActivityQry.setPageNum(1);
		pageActivityQry.setPageSize(1000);
		List<String> statusList = Lists.newArrayList(YanActivityStatusE.ready.getCode(), YanActivityStatusE.started.getCode());
		pageActivityQry.setActivityStatusList(statusList);
		List<ActivityVo> activityVos = yanActivityService.listActivity(pageActivityQry);
		activityVos.forEach(x->{
			LocalDateTime now = LocalDateTime.now();
			if(YanActivityStatusE.ready.getCode().equals(x.getActivityStatus()) && x.getStartTime().isBefore(now) && x.getEndTime().isAfter(now)){
				x.setActivityStatus(YanActivityStatusE.started.getCode());
				yanActivityService.saveActivity(x, false);
			}else if(YanActivityStatusE.started.getCode().equals(x.getActivityStatus()) && x.getEndTime().isBefore(now)){
				x.setActivityStatus(YanActivityStatusE.ended.getCode());
				yanActivityService.saveActivity(x, false);
			}
		});
	}
}
